package net.qhzw.geologr5.gis.service.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.geologr5.gis.api.system.entity.User;
import net.qhzw.geologr5.gis.common.framework.utils.JsonResult;
import net.qhzw.geologr5.gis.common.framework.utils.StringUtils;
import net.qhzw.geologr5.gis.common.security.utils.SecurityUtils;
import net.qhzw.geologr5.gis.service.system.component.FileTemplate;
import net.qhzw.geologr5.gis.service.system.entity.Legend;
import net.qhzw.geologr5.gis.service.system.entity.ProjectLegend;
import net.qhzw.geologr5.gis.service.system.mapper.LegendMapper;
import net.qhzw.geologr5.gis.service.system.mapper.ProjectLegendMapper;
import net.qhzw.geologr5.gis.service.system.mapper.UserMapper;
import net.qhzw.geologr5.gis.service.system.query.ProjectLegendQuery;
import net.qhzw.geologr5.gis.service.system.service.LegendService;
import net.qhzw.geologr5.gis.service.system.service.ProjectLegendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class ProjectLegendServiceImpl implements ProjectLegendService {

    @Autowired
    private ProjectLegendMapper projectLegendMapper;

    @Autowired
    private FileTemplate fileTemplate;

    @Autowired
    private LegendMapper legendMapper;

    @Autowired
    private UserMapper userMapper;

    /***
     * 创建项目图例 || 修改
     * @param projectLegend
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult create(ProjectLegend projectLegend, MultipartFile file) {
        log.info("创建项目图例==ProjectLegendServiceImpl==method:create==param: {}", JSON.toJSONString(projectLegend));
        try {
            if (projectLegend.getId() != null && projectLegend.getId() > 0) {
                //修改
                int count = projectLegendMapper.updateById(projectLegend);
                if (count > 0) {
                    return JsonResult.success();
                }
                return JsonResult.error("修改失败");
            }
            if (projectLegend.getCreateTime() == null) {
                projectLegend.setCreateTime(new Date());
            }
            if (file == null) {
                return JsonResult.error("请上传图例");
            }
            //上传图例信息
            String path = fileTemplate.fileUpload(file);
            if (path == null || path.length() <= 0) {
                return JsonResult.error("请上传图例失败");
            }
            projectLegend.setIcon(path);
            Integer userId = SecurityUtils.getLoginUser().getUserId();
            User user = userMapper.selectById(userId);
            if (StringUtils.isNull(user)) {
                return JsonResult.error("用户信息不存在");
            }
            projectLegend.setFounder(user.getRealname());
            int count = projectLegendMapper.insert(projectLegend);
            if (count > 0) {
                return JsonResult.success();
            }
            return JsonResult.error("新增失败");
        } catch (Exception e) {
            log.error("创建项目图例失败==ProjectLegendServiceImpl==method:create==errMsg: {}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }

    }


    /***
     * 项目图例查询
     * @param query
     * @return
     */
    @Override
    public JsonResult find(ProjectLegendQuery query) {
        log.info("项目图例查询==ProjectLegendServiceImpl==method:find==param: {}", JSON.toJSONString(query));
        PageHelper.startPage(query.getPage(), query.getLimit());
        Map<String, Object> result = new HashMap<>();
        try {
            List<ProjectLegend> projectLegendList = projectLegendMapper.find(query);
            PageInfo<ProjectLegend> pageInfo = new PageInfo<>(projectLegendList);
            if (pageInfo != null) {
                result.put("list", pageInfo.getList());
                result.put("total", pageInfo.getTotal());
            }
            return JsonResult.success(result);
        } catch (Exception e) {
            log.error("项目图例查询失败==ProjectLegendServiceImpl==method:find==errMsg: {}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }
    }


    /***
     * 项目图例删除
     * @param ids
     * @return
     */
    @Override
    public JsonResult delete(Integer[] ids) {
        try {
            List<Integer> idList = Arrays.asList(ids);
            int count = projectLegendMapper.deleteBatchIds(idList);
            if (count > 0) {
                return JsonResult.success();
            }
            return JsonResult.error();
        } catch (Exception e) {
            log.error("项目图例删除==ProjectLegendServiceImpl==method:delete==errMsg: {}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }
    }


    /***
     * 查询id不等于当前这条记录的图例
     * @param query
     * @return
     */
    @Override
    public JsonResult findLegend(ProjectLegendQuery query) {
        log.info("项目图例查询==ProjectLegendServiceImpl==method:findLegend==param: {}", JSON.toJSONString(query));
        PageHelper.startPage(query.getPage(), query.getLimit());
        Map<String, Object> result = new HashMap<>();
        try {
            List<ProjectLegend> projectLegendList = projectLegendMapper.findLegend(query);
            PageInfo<ProjectLegend> pageInfo = new PageInfo<>(projectLegendList);
            if (pageInfo != null) {
                result.put("list", pageInfo.getList());
                result.put("total", pageInfo.getTotal());
            }
            return JsonResult.success(result);
        }catch (Exception e){
            log.error("项目图例查询==ProjectLegendServiceImpl==method:findLegend==errMsg: {}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }

    }
}
